﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OT.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Using Directives

    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Reflection;
    using System.Threading;

    using Enums;

    #endregion

    /// <summary>
    /// The ot.
    /// </summary>
    public class OT
    {
        private static readonly Identity Identity = Identity.Create(System.Reflection.MethodBase.GetCurrentMethod());

        #region Public Methods

        /// <summary>
        /// The close.
        /// </summary>
        public static void Close()
        {
            Logging.Log(Identity, ELogLevel.Info, () => "OT.Close()");

            // Finish background tasks
            Singleton<QueueBackgroundWorker>.Instance.Terminate();

            // Kill all conime processes (VS 2008 workaround)
            var staleConIMEProcesses = from p in Process.GetProcessesByName("conime") 
                            where p.StartInfo.EnvironmentVariables.ContainsKey("vs90comntools") 
                            select p;
            foreach (var p in staleConIMEProcesses)
            {
                Logging.Log(Identity, ELogLevel.Info, () => string.Format("Killing stale conime.exe {0} (VS 2008 workaround)", p.Id));
                Devel.NoThrow(p.Kill);
            }

            // Exit forcefully
            Environment.Exit(0);
        }

        /// <summary>
        /// The open.
        /// </summary>
        public static void Open()
        {
            Logging.Log(Identity, ELogLevel.Info, () => "OT.Open()");
        }

        #endregion
    }
}